const httpUtils = require('../../../utils/httpUtils')
const ui = require('../../../utils/ui')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    oldPwd: '',
    newPwd: '',
    confirmPwd: ''
  },

  //输入旧密码
  inputOldPwd: function (e) {
    this.setData({
      oldPwd: e.detail.value
    })
  },
  //输入新密码
  inputNewPwd: function (e) {
    this.setData({
      newPwd: e.detail.value
    })
  },
  //输入新密码
  inputConfirmPwd: function (e) {
    this.setData({
      confirmPwd: e.detail.value
    })
  },

  confirm: function (e) {
    let oldPwd = this.data.oldPwd;
    let newPwd = this.data.newPwd;
    let confirmPwd = this.data.confirmPwd;
    console.log('旧密码:' + oldPwd)
    console.log('新密码:' + newPwd)
    console.log('确认密码:' + confirmPwd)
    if (undefined == oldPwd || '' == oldPwd || null == oldPwd) {
      wx.showToast({
        title: '请输入旧密码',
        icon: 'error'
      })
      return;
    }
    if (undefined == newPwd || '' == newPwd || null == newPwd) {
      wx.showToast({
        title: '请输入新密码',
        icon: 'error'
      })
      return;
    }
    if (undefined == confirmPwd || '' == confirmPwd || null == confirmPwd) {
      wx.showToast({
        title: '请再次确认新密码',
        icon: 'error'
      })
      return;
    }
    if (newPwd != confirmPwd) {
      wx.showToast({
        title: '新密码不一致',
        icon: 'error'
      })
    }
    let obj = {
      method: 'POST',
      showLoading: true,
      url: 'user/updateLoginPwd',
      data: {
        'oldPwd': oldPwd,
        'newPwd': newPwd,
        'confirmPwd': confirmPwd
      }
    }
    httpUtils.request(obj).then(res => {
      if (res.data.retCode == 0) {
        this.setData({
          oldPwd: '',
          newPwd: '',
          confirmPwd: ''
        });
        // wx.redirectTo({
        //   url: '/pages/my-pages/login/login',
        // });
        wx.setStorageSync('token', '');
        wx.reLaunch({
          url: '/pages/my-pages/login/login',
        })
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})